import { _decorator, Component, instantiate, Node, Prefab } from 'cc';
const { ccclass, property } = _decorator;

enum BlockType {
    BLOCK_NONE,
    BLOCK_WHITE
}

@ccclass('GroundGenerator')
export class GroundGenerator extends Component {

    @property(Prefab)
    public box: Prefab = null;

    @property
    public roadLength: number = 50;

    private _road:BlockType[] = []

    start() {
        // this.generateGround();
    }

    update(deltaTime: number) {
        
    }

    generateGround() {
        this.node.removeAllChildren();
        this._road = [];
        
        this._road.push(BlockType.BLOCK_WHITE);
        for (let i = 1; i < this.roadLength; i++) {
            if(this._road[i - 1] === BlockType.BLOCK_NONE) {
                this._road.push(BlockType.BLOCK_WHITE);
            } else {
                this._road.push(Math.round(Math.random()))
            }
        }

        for (let j = 0; j < this.roadLength; j++) {
            if (this._road[j] === BlockType.BLOCK_WHITE) {
                const node = instantiate(this.box);
                node.setParent(this.node);
                node.setPosition(40 * j, 0, 0);
            }
        }
    }

    isGameOver(step: number) {
        return this._road[step] === BlockType.BLOCK_NONE || step >= this.roadLength;
    }
}
